package com.franklin.java.api.psi.data.resolver;

import com.franklin.java.api.psi.data.PsiData;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiForeachStatement;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiTreeUtil;

import java.util.Objects;

/**
 * java for each循环解析器
 * @author Franklin
 * @since 2021/09/04 22:37
 */
public class ForeachLoopResolver implements PsiDataResolver {
    @Override
    public PsiData resolve(String name, String value, PsiElement dataElement) {
        PsiForeachStatement psiForeachStatement = PsiTreeUtil.getParentOfType(dataElement, PsiForeachStatement.class);
        if (Objects.isNull(psiForeachStatement)){
            return null;
        }
        PsiParameter iterationParameter = psiForeachStatement.getIterationParameter();
        if (iterationParameter.getName().equals(name)){
            PsiType psiType = iterationParameter.getType();
            String shortName = psiType.getPresentableText();
            String fullName = psiType.getCanonicalText();
            return new PsiData(shortName,name,iterationParameter,fullName,dataElement);
        }

        return null;
    }
}
